package com.dlc.shop.systemtest.api.amount;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dlc.shop.bean.app.dto.CouponDto;
import com.dlc.shop.bean.model.OfflineHandleEvent;
import com.dlc.shop.bean.param.OfflineHandleEventAuditParam;
import com.dlc.shop.common.util.Json;
import com.dlc.shop.coupon.common.model.Coupon;
import com.dlc.shop.coupon.common.model.CouponUser;
import com.dlc.shop.systemtest.constants.Constant;
import com.dlc.shop.systemtest.util.CheckUtil;
import com.dlc.shop.systemtest.util.HttpUtil;

import java.util.List;

/**
 * @author lh
 */
public class CouponApi {

    public static Long save(Coupon coupon){
        String url = Constant.SHOP_URL+"/admin/coupon";
        return HttpUtil.post(url,coupon,Long.class,true);
    }

    public static Long platformSave(Coupon coupon){
        String url = Constant.PLATFORM_URL+"/platform/coupon";
        return HttpUtil.post(url,coupon,Long.class,true);
    }

    public static String receive(Long couponId){
        String url = Constant.API_URL+"/p/myCoupon/receive";
        HttpRequest post = HttpRequest.post(url);
        post.header("authorization", Constant.API_TOKEN);
        post.header("content-type","application/json; charset=UTF-8");
        HttpResponse response = post.body(Json.toJsonString(couponId)).execute();
        return CheckUtil.checkAndGetObject(response,String.class);
    }


    public static List<CouponDto> list(){
        String url = Constant.API_URL+"/p/myCoupon/listCouponIds";
        List list = HttpUtil.get(url, List.class);
        return  JSON.parseArray(JSONObject.toJSONString(list), CouponDto.class);
    }

    /**
     * 用户删除优惠券
     */
    public static void userDelete(Long couponId) {
        String url = Constant.API_URL + "/p/myCoupon/delCoupon/" + couponId;
        HttpUtil.delete(url);
    }

    public static Coupon getCouponById(Long couponId){
        String url = Constant.SHOP_URL+"/admin/coupon/info/"+couponId;
        return HttpUtil.get(url,Coupon.class);
    }

    /**
     * 修改优惠券
     */
    public static void update(Coupon coupon) {
        String url = Constant.SHOP_URL + "/admin/coupon";
        HttpUtil.put(url, coupon);
    }

    /**
     * 店铺删除优惠券
     */
    public static void delete(Long couponId) {
        String url = Constant.SHOP_URL + "/admin/coupon";
        HttpUtil.delete(url, couponId);
    }

    /**
     * 平台违规下架优惠券
     */
    public static void offline(OfflineHandleEvent offlineHandleEvent) {
        String url = Constant.PLATFORM_URL + "/platform/coupon/offline";
        HttpUtil.post(url, offlineHandleEvent);
    }

    /**
     * 商家根据优惠券ID获取下架信息
     */
    public static OfflineHandleEvent getOfflineHandleEvent(Long couponId) {
        String url = Constant.SHOP_URL + "/admin/coupon/getOfflineHandleEventByCouponId/" + couponId;
        return HttpUtil.get(url, OfflineHandleEvent.class);
    }

    /**
     * 商家根据下架信息申请上架
     */
    public static void applyOnline(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        String url = Constant.SHOP_URL + "/admin/coupon/auditApply";
        HttpUtil.post(url, offlineHandleEventAuditParam);
    }

    /**
     * 平台根据优惠券ID获取下架信息
     */
    public static OfflineHandleEvent getOfflineHandleEventByPlatform(Long couponId) {
        String url = Constant.PLATFORM_URL + "/platform/coupon/getOfflineHandleEventByCouponId/" + couponId;
        return HttpUtil.get(url, OfflineHandleEvent.class);
    }

    /**
     * 平台根据下架信息审核
     */
    public static void audit(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        String url = Constant.PLATFORM_URL + "/platform/coupon/auditCoupon";
        HttpUtil.post(url, offlineHandleEventAuditParam);
    }

    /**
     * 根据用户id获取优惠券信息
     * @param userId
     * @return
     */
    public static List<CouponUser> getCouponUserByUserId(String userId){
        String url = Constant.SHOP_URL+"/admin/couponUser/info/"+userId;
        Object result = HttpUtil.get(url, Object.class);
        List<CouponUser> couponUsers = JSON.parseArray(result.toString(), CouponUser.class);
        return couponUsers;
    }

    /**
     * 根据优惠券ID获取平台优惠券信息
     */
    public static Coupon getPlatformCouponById(Long couponId) {
        String url = Constant.PLATFORM_URL + "/platform/coupon/info/" + couponId;
        return HttpUtil.get(url, Coupon.class);
    }

    /**
     * 修改平台优惠券
     */
    public static void updatePlatformCoupon(Coupon coupon) {
        String url = Constant.PLATFORM_URL + "/platform/coupon";
        HttpUtil.put(url, coupon);
    }

    /**
     * 删除平台优惠券
     */
    public static void deletePlatformCoupon(Long couponId) {
        String url = Constant.PLATFORM_URL + "/platform/coupon";
        HttpUtil.delete(url, couponId);
    }

}
